/*
	Written By Pradipna Nepal
	www.pradsprojects.com

	Copyright (C) 2012 Prads Projects
	Please read COPYING.txt included along with this source code for more detail.
	If not included, see http://www.gnu.org/licenses/
*/

#include "libSymbolTable.h"

bool SymbolTable::createSymbolTable(vector<TokenModule> *modules, SblTable *sOut) {
	if (!createFunctionTable(modules, &sOut->funcTable)) return false;
	createIdentTable(modules, &sOut->funcTable, &sOut->identTable);
	if (!createStructTable(&(*modules)[0], &sOut->structTable)) return false;

	int size = 0;

	for (IdentTable::const_iterator it = sOut->identTable[0].begin(); it != sOut->identTable[0].end(); ++it) {
		if (it->second.type == "var")
			size += it->second.size * 2;
	}

	(*modules)[0].size = size;
	size = 0;

	for (unsigned int i = 0; i < modules->size(); ++i) {
		if ((*modules)[i].name == "main") {
			for (IdentTable::const_iterator it = sOut->identTable[i].begin(); it != sOut->identTable[i].end(); ++it) {
				if (it->second.type == "var")
					size += it->second.size * 2;
			}

			(*modules)[i].size = size;
		}
	}

	return true;
}